package com;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.Transfer;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferProgress;
import com.amazonaws.services.s3.transfer.Upload;
import com.interfaces.UploadCallback;

import java.io.InputStream;

public class UploadFile {

    private TransferManager transferManager;
    private PutObjectRequest por;
    private UploadCallback callback;
    private String bucketName;
    private String key;
    private ObjectMetadata meta;

    public UploadFile() {
    }

    public UploadFile(TransferManager transferManager,
                      String bucketName,
                      String filePath,
                      InputStream inputStream,
                      ObjectMetadata meta) {
        this.transferManager = transferManager;
        por = new PutObjectRequest(bucketName, filePath, inputStream, meta);
    }

    public UploadFile setObjectMetadata(ObjectMetadata meta) {
        this.meta = meta;
        return this;
    }

    public UploadFile setTransferManager(TransferManager transferManager) {
        this.transferManager = transferManager;
        return this;
    }

    public UploadFile setKey(String key) {
        this.key = key;
        return this;
    }

    public UploadFile setBucketName(String bucketName) {
        this.bucketName = bucketName;
        return this;
    }

    public UploadFile setCallback(UploadCallback callback) {
        this.callback = callback;
        return this;
    }

    /**
     * 开始上传
     */
    public void upload() {
        try {
            // 设置此文件为公共读取
            por.withCannedAcl(CannedAccessControlList.PublicRead);

//            void setObjectAcl(String var1, String var2, AccessControlList var3) throws SdkClientException, AmazonServiceException;
//
//            void setObjectAcl(String var1, String var2, String var3, AccessControlList var4) throws SdkClientException, AmazonServiceException;
//
//            void setObjectAcl(String var1, String var2, String var3, CannedAccessControlList var4) throws SdkClientException, AmazonServiceException;
//
//            void setObjectAcl(SetObjectAclRequest var1) throws SdkClientException, AmazonServiceException;


            Upload upload = transferManager.upload(por);
//            XferMgrProgress.showTransferProgress(upload);
            showTransferProgress(upload);
            XferMgrProgress.waitForCompletion(upload);
//            // 设置此文件为公共读取
//            transferManager.getAmazonS3Client()
//                    .setObjectAcl(
//                        por.getBucketName(),
//                        por.getKey(),
//                        CannedAccessControlList.PublicRead
//                    );
        } catch (AmazonServiceException e) {
            System.err.println("error file = " + por.getKey());
            System.err.println(e.toString());
        }

    }

    private void showTransferProgress(Upload upload) {
        System.out.println(upload.getDescription());
        TransferProgress progress = upload.getProgress();
        do {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                return;
            }
            long so_far = progress.getBytesTransferred();
            long total = progress.getTotalBytesToTransfer();
            double pct = progress.getPercentTransferred();
            System.out.print('\r');
            System.out.print("%n% 进度 "+pct+"%");
        } while (!upload.isDone());
        // print the final state of the transfer.
        Transfer.TransferState xfer_state = upload.getState();
        System.out.println(" ");
        System.out.println("upload " + xfer_state);
        switch (xfer_state) {
            case Completed:
                callback.complete();
                break;
            case Failed:
//                resetUpload();
                callback.failed();
                break;
            case InProgress:

                break;
            case Canceled:
                break;
            case Waiting:
                break;
        }

    }

}
